package com.sanyou.service.impl;

import com.sanyou.pojo.Equipment;
import com.sanyou.pojo.Factory;
import com.sanyou.service.DataAuthorityService;
import com.sanyou.service.EquipmentService;
import com.sanyou.service.FactoryService;
import com.sanyou.service.ResourceService;
import com.sanyou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * AuthorityService接口的实现类
 */
@Service
public class DataAuthorityServiceImpl implements DataAuthorityService {
    @Autowired
    EquipmentService equipmentService;

    @Autowired
    FactoryService factoryService;

    @Autowired
    RoleService roleService;

    @Autowired
    ResourceService resourceService;

    @Override
    public boolean checkAuthorityOfEquipmentById(String userId, String equipId) {
        //若具有超管权限，则全部放行
        if(roleService.iSAdminByUserId(userId)){
            return true;
        }

        //数据校验
        if (StringUtils.isEmpty(equipId)){
            return false;
        }

        //获取用户拥有访问权限的所有设备
        List<Equipment> equipments = equipmentService.listEquipmentByUserId(userId);

        //权限校验
        if(equipments!=null){
            for (Equipment equipment:equipments){
                if(equipId.equals(equipment.getId())){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean checkAuthorityOfEquipmentByNo(String userId, String equipNo) {
        Equipment equipment = equipmentService.getEquipmentByCode(equipNo);
        if(equipment!=null){
            return checkAuthorityOfEquipmentById(userId,equipment.getId());
        }
        return false;
    }

    @Override
    public boolean checkAuthorityOfSubFactory(String userId, String subFactoryId) {
        //若具有超管权限，则全部放行
        if(roleService.iSAdminByUserId(userId)){
            return true;
        }

        //数据校验
        if (StringUtils.isEmpty(subFactoryId)){
            return false;
        }

        //获取用户拥有访问权限的所有设备
        List<Factory> factories = factoryService.getUserEquipFactory(userId);

        //权限校验
        if(factories!=null){
            for (Factory factory:factories){
                if(subFactoryId.equals(factory.getId())){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean checkAuthorityOfFactory(String userId, String factoryId) {
        //若具有超管权限，则全部放行
        if(roleService.iSAdminByUserId(userId)){
            return true;
        }

        //数据校验
        if (StringUtils.isEmpty(factoryId)){
            return false;
        }

        //如果用户拥有该工厂下一个及以上车间的数据访问权限，则认为用户对该工厂具有数据访问权限
        List<Factory> subFactories = factoryService.getSubFactory(userId,factoryId);
        if(subFactories!=null && subFactories.size()>0){
            return true;
        }
        return false;
    }
}
